package com.logos.ruralrevitalization.tools;

import com.logos.ruralrevitalization.converter.AgriculturalProductConverter;
import com.logos.ruralrevitalization.converter.AuthUserConverter;
import com.logos.ruralrevitalization.entity.AgriculturalProduct;
import com.logos.ruralrevitalization.entity.AuthUser;
import com.logos.ruralrevitalization.enums.IsDeleteFlagEnum;
import com.logos.ruralrevitalization.mapper.AgriculturalProductMapper;
import com.logos.ruralrevitalization.req.AgriculturalProductReq;
import com.logos.ruralrevitalization.req.chat.AgriculturalProductQuery;
import com.logos.ruralrevitalization.resp.AgriculturalProductResp;
import com.logos.ruralrevitalization.resp.AuthUserResp;
import com.logos.ruralrevitalization.service.AgriculturalProductService;
import com.logos.ruralrevitalization.service.AuthUserService;
import com.logos.ruralrevitalization.service.ProductOrderService;
import com.logos.ruralrevitalization.service.ShoppingCartService;
import com.logos.ruralrevitalization.utils.RuralLoginUtil;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;


/**
 * 农户工具类
 */
@Component
public class RuralTools {

    @Resource
    private AgriculturalProductMapper agriculturalProductMapper;

    @Resource
    private AuthUserService authUserService;

    @Resource
    private AgriculturalProductService agriculturalProductService;

    @Resource
    private RuralLoginUtil ruralLoginUtil;

    @Tool(description = "查询当前农户个人信息")
    public AuthUserResp queryInfo() {
        AuthUser authUser = authUserService.queryById(Long.valueOf(ruralLoginUtil.getId()));
        return AuthUserConverter.INSTANCE.convertEntityToResp(authUser);
    }

    @Tool(description = "查询当前农户上架的农产品")
    public List<AgriculturalProductResp> queryRuralProd() {
        Long userId = Long.valueOf(ruralLoginUtil.getId());
        return agriculturalProductService.queryRuralProd(userId);
    }


    @Tool(description = "根据条件查询农产品")
    public List<AgriculturalProductResp> queryAgriculturalProduct(@ToolParam(required = false, description = "农产品查询条件") AgriculturalProductQuery agriculturalProductQuery) {
        AgriculturalProduct agriculturalProduct = AgriculturalProductConverter.INSTANCE.convertQueryToEntity(agriculturalProductQuery);

        List<AgriculturalProduct> agriculturalProductList = agriculturalProductMapper.queryByConditionWithChat(agriculturalProduct, agriculturalProductQuery.getSorts());
        if (agriculturalProductList == null || agriculturalProductList.isEmpty()) {
            return null;
        }
        List<AgriculturalProductResp> agriculturalProductRespList = new LinkedList<>();
        for (AgriculturalProduct product : agriculturalProductList) {
            AgriculturalProductResp agriculturalProductResp = AgriculturalProductConverter.INSTANCE.convertEntityToResp(product);
            agriculturalProductRespList.add(agriculturalProductResp);
        }
        return agriculturalProductRespList;
    }

    @Tool(description = "根据农产品id查询农产品")
    public AgriculturalProductResp queryRuralProdById(@ToolParam(description = "农产品id") Long id) {
        return agriculturalProductService.queryById(id);
    }

    @Tool(description = "上架农产品,返回true表示上架成功，false表示上架失败")
    public Boolean addAgriculturalProduct(@ToolParam(description = "农产品名称") String name,
                                          @ToolParam(description = "农产品描述") String description,
                                          @ToolParam(description = "农产品的图片") String avatar,
                                          @ToolParam(description = "农产品类型,1果蔬，2畜产，3水产") Integer productType,
                                          @ToolParam(description = "农产品价格") Double price,
                                          @ToolParam(description = "生产日期") Date productionDate,
                                          @ToolParam(description = "过期日期") Date expirationDate) {
        AgriculturalProductQuery agriculturalProductQuery = new AgriculturalProductQuery();
        agriculturalProductQuery.setName(name);
        agriculturalProductQuery.setDescription(description);
        agriculturalProductQuery.setAvatar(avatar);
        agriculturalProductQuery.setProductType(productType);
        agriculturalProductQuery.setPrice(price);
        agriculturalProductQuery.setProductionDate(productionDate);
        agriculturalProductQuery.setExpirationDate(expirationDate);
        agriculturalProductQuery.setSupplierId(Long.valueOf(ruralLoginUtil.getId()));
        AgriculturalProduct agriculturalProduct = AgriculturalProductConverter.INSTANCE.convertQueryToEntity(agriculturalProductQuery);
        agriculturalProduct.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
        agriculturalProduct.setSalesVolume(0);
        return agriculturalProductMapper.insert(agriculturalProduct) > 0;
    }

    @Tool(description = "下架农产品,返回true表示下架成功，false表示下架失败")
    public Boolean downAgriculturalProduct(@ToolParam(description = "id") Long id) {
        AgriculturalProductReq agriculturalProductReq = new AgriculturalProductReq();
        agriculturalProductReq.setId(id);
        agriculturalProductReq.setSupplierId(Long.valueOf(ruralLoginUtil.getId()));
        return agriculturalProductService.down(agriculturalProductReq);
    }

}
